const app = getApp()
// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    swipertab: [{ name: '已接单', orderStatus: 0 }, { name: '已完成', orderStatus: 1 }, { name: '已取消', orderStatus: 2 }],
    
    orderList0:"",
    orderList1:"",
    orderList2:"",

    //提示框
    isShowConfirm: false ,
    workerRemark:"",
    modalHidden0: true,
    word:''
    

  },

  ///////////////////理由信息弹框
  modalTap0: function (e) {
    var that = this
    var remark = e.currentTarget.dataset.remark
    var date = e.currentTarget.dataset.date
    console.log("remark:", e.currentTarget.dataset.remark)
    
    that.setData({
      remark: remark,
      date: date,
      purl1: "../../images/why.png",
      purl2: "../../images/sad.png",
      modalHidden0: false
    });
  },
  modalHide0: function (e) {
    this.setData({
      modalHidden0: true
    })
  },
  ////////////


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo()
    this.orderShow()
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
    this.orderShow()
  },

  orderShow: function () {
    let that = this
    switch (this.data.currtab) {      
      case 0:
        that.yijiedanShow()
        break
      case 1:
        that.alreadyShow()
        break
      case 2:
        that.lostShow()
        break
    }
  },

 
  yijiedanShow: function () {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderbyworker',
      method: 'GET',
      data: { "workerOpenid": openid, orderStatus: 1 },//传入的参数
      success: function (res) {
        var list = res.data.orders;

        console.log("list", list)
        if (list == null) {
          var toastText = '没有相关信息' ;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            orderList0: list
          });
          
        }
      }
    })
  },
  alreadyShow: function () {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderbyworker',
      method: 'GET',
      data: { "workerOpenid": openid, orderStatus: 2 },//传入的参数
      success: function (res) {
        var list = res.data.orders;

        console.log("list", list)
        if (list == null) {
          var toastText = '无数据' ;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            orderList1: list
          });
        }
      }
    })
  },

  lostShow: function () {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderbyworker',
      method: 'GET',
      data: { "workerOpenid": openid, orderStatus: 3 },//传入的参数
      success: function (res) {
        var list = res.data.orders;

        console.log("list", list)
        if (list == null) {
          var toastText = '无数据';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            orderList2: list
          });
        }
      }
    })
  },
//////////////////////////////////////////////////////////
  setValue: function (e) {
   // wx.setStorageSync("workerRemark", e.detail.value)  
    this.setData({
      workerRemark: e.detail.value
    });
  },
  cancel0: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
    })
  },
  confirmAcceptance: function (e) {
    /////////////////
    console.log("this.data.word", this.data.word)
    console.log("this.data.workerRemark", this.data.workerRemark)
    if (this.data.word == '' && this.data.workerRemark==""){
      this.setData({
        workerRemark: "未有留言信息"
      });

    }
    ////////////
    var that = this
      
    console.log("workerRemark:" + that.data.workerRemark )
    that.setData({
      isShowConfirm: false,
    })
    wx.request({
      url: app.globalData.baseUrl +'/order/workercancel',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        orderId: wx.getStorageSync("workOrderId"),
        workerRemark: that.data.workerRemark 
      },

      success: function (res) {
        var result = res.data.success;
        var toastText = "取消成功！";
        if (result != true) {
          toastText = "取消失败";
        } else {

          that.yijiedanShow();
        }
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 2000
        });
      }
    })
  },
  ////////////////////////////


  cancel: function (e) {
    var that = this;
    console.log("orderId:" + e.currentTarget.dataset.orderid)
    wx.setStorageSync("workOrderId", e.currentTarget.dataset.orderid)
    wx.showModal({
      title: '提示',
      content: '确定要取消接单吗？',

      success: function (sm) {
        if (sm.confirm) {
          //////////
          that.setData({
            isShowConfirm: true
          });
          console.log("isShowConfirm:" + that.data.isShowConfirm)
          /////////////

         /* wx.request({
            url: 'http://localhost:8080/demo/order/workercancell',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: { "orderId": e.currentTarget.dataset.orderid ,
              workerRemark: wx.getStorageSync("workerRemark")
            },

            success: function (res) {
              var result = res.data.success;
              var toastText = "取消成功！";
              if (result != true) {
                toastText = "取消失败";
              } else {

                that.yijiedanShow();
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });
            }
          })*/
        }
      }
    })
  },
  getDetail: function (e) {
    var url1 = '../order/orderdetail/orderdetail?orderId=' + e.currentTarget.dataset.orderid
    console.log("url1:" + url1)
    wx.navigateTo({
      url: url1
    })

  },
  finish:function(e){
    var that = this;
    console.log("orderId:" + e.currentTarget.dataset.orderid)
    wx.showModal({
      title: '提示',
      content: '确定已完成该订单了吗？',

      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.baseUrl +'/order/finish',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: { "orderId": e.currentTarget.dataset.orderid },



            success: function (res) {
              var result = res.data.success;
              var toastText = "确认成功！";
              if (result != true) {
                toastText = "确认失败！";
              } else {

                that.yijiedanShow();
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });
            }
          })
        }
      }
    })
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({

      orderList0: null,
      orderList1: null,
      orderList2: null
      
    });
    setTimeout(function () {
      that.orderShow()
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 1500);

  },

  ////////////////////拨号
  Tel: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel, //这个是我的手机号，模拟测试
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  }





})
